package com.k7.mobile.platform.web.action.admin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.k7.mobile.platform.bo.ILogBO;
import com.k7.mobile.platform.dataobject.UserDO;
import com.k7.mobile.platform.web.action.BaseAction;
import com.k7.mobile.platform.web.support.Constants;
import com.qike.java.pojo.User;

/**
 * 用户后台管理
 * @author lx
 *
 */
public class UserManageAction extends BaseAction{
	
	private static final long serialVersionUID = -5581897070979714532L;

	private ILogBO logBO;
	
	private Long userId;
	private String userNick;
	private Integer status;
	private String oper;
	private Integer adminRole;
	private Integer page;
	private String forbidBeginTime;
	private String forbidEndTime;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private List<UserDO> list;
	private UserDO queryDO;
	private Map<Long, User> userMap;
	
	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getUserNick() {
		return userNick;
	}

	public void setUserNick(String userNick) {
		this.userNick = userNick;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public List<UserDO> getList() {
		return list;
	}

	public UserDO getQueryDO() {
		return queryDO;
	}

	
	public Integer getAdminRole() {
		return adminRole;
	}

	public void setAdminRole(Integer adminRole) {
		this.adminRole = adminRole;
	}

	public void setLogBO(ILogBO logBO) {
		this.logBO = logBO;
	}

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public Map<Long, User> getUserMap() {
		return userMap;
	}

	public String getForbidBeginTime() {
		return forbidBeginTime;
	}

	public void setForbidBeginTime(String forbidBeginTime) {
		this.forbidBeginTime = forbidBeginTime;
	}

	public String getForbidEndTime() {
		return forbidEndTime;
	}

	public void setForbidEndTime(String forbidEndTime) {
		this.forbidEndTime = forbidEndTime;
	}

	/**
	 * 用户列表页
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception{
		queryDO = new UserDO();
		
		if(userId != null)
			queryDO.setUserId(userId);
		if(userNick != null)
			queryDO.setUserNick(userNick);
		if(status != null)
			queryDO.setStatus(status);
			
		//设置分页
		if(page == null)
			page = 1;
		queryDO.setToPage(page);
		
		list = super.getUserBO().getUserList(queryDO);
		if(oper != null && oper.equals("upd"))
			oper = "success";
		
		List<String> uids = new ArrayList<String>();
		for(UserDO user : list){
			if(user.getUserId() != null && user.getUserId() != 0)
				uids.add(user.getUserId().toString());
		}
		userMap = getUsersByUids(uids);
		
		return SUCCESS;
	}
	
	/**
	 * 用户黑名单设置
	 * @return
	 * @throws Exception
	 */
	public String upd() throws Exception{
		if(userId == null)
			return ERROR;
		
		User user = getUser();
		
		UserDO userDO = new UserDO();
		userDO.setUserId(userId);
		if(status != null)
			userDO.setStatus(status);
		if(adminRole != null)
			userDO.setAdminRole(adminRole);
		
		super.getUserBO().updateUser(userDO);
		
		try{
			logBO.saveLog(user.getUid().longValue(), userId, "用户管理", Constants.LOG_OPT_TYPE_UPDATE, Constants.LOG_TYPE_OTHER, "用户管理:" + status + "," + adminRole);
		}catch(Exception e){
			log.error("ThreadReplyManageAction:upd error,", e);
		}
	
		return SUCCESS;
	}
	
	/**
	 * 用户禁言设置
	 * @return
	 * @throws Exception
	 */
	public String userForbid() throws Exception{
		if(userId == null)
			return ERROR;
		
		User user = getUser();
		
		UserDO userDO = new UserDO();
		userDO.setUserId(userId);
		
		if(!StringUtils.isBlank(forbidBeginTime))
			userDO.setForbidBeginTime(sdf.parse(forbidBeginTime + " 00:00:00"));
		if(!StringUtils.isBlank(forbidEndTime))
			userDO.setForbidEndTime(sdf.parse(forbidEndTime + " 23:59:59"));
		
		super.getUserBO().updateUser(userDO);
		
		try{
			logBO.saveLog(user.getUid().longValue(), userId, "用户管理", Constants.LOG_OPT_TYPE_UPDATE, Constants.LOG_TYPE_OTHER, "用户禁言管理:" + forbidBeginTime + "," + forbidEndTime);
		}catch(Exception e){
			log.error("ThreadReplyManageAction:upd error,", e);
		}
	
		return SUCCESS;
	}
}
